import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/file'

const api = {

    /**
     * 新增流程附件管理
     * @param file
     * @returns {Promise<*|undefined>}
     */
    createFile: (file) =>
      axiosClient().post(`${requestUrl}/addInfo`, file),

    /**
     * 根据ID删除流程附件管理
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteFileById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新流程附件管理
     * @param id
     * @param file
     * @returns {Promise<*|undefined>}
     */
    updateFileFull: (id, file) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, file),

    /**
     * 根据ID部分更新流程附件管理
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateFilePartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询流程附件管理
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getFileById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询流程附件管理
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getFilePage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询流程附件管理
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getFileList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.instanceId) {
            params.append('instanceId', businessParam.instanceId)
        }
        if (businessParam.workItemId) {
            params.append('workItemId', businessParam.workItemId)
        }
        if (businessParam.fileName) {
            params.append('fileName', businessParam.fileName)
        }
        if (businessParam.filePath) {
            params.append('filePath', businessParam.filePath)
        }
        if (businessParam.fileSize) {
            params.append('fileSize', businessParam.fileSize)
        }
        if (businessParam.fileType) {
            params.append('fileType', businessParam.fileType)
        }
        if (businessParam.uploadUserId) {
            params.append('uploadUserId', businessParam.uploadUserId)
        }
        if (businessParam.uploadTime) {
            params.append('uploadTime', businessParam.uploadTime)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
